package com.nervenets.web.controller.handler.user.user;

import com.nervenets.web.controller.handler.Handler;
import com.nervenets.web.controller.handler.common.TokenParams;
import com.nervenets.web.controller.handler.user.UserModel;
import com.nervenets.web.entity.MessageCode;
import com.nervenets.web.enumeration.WealthType;
import com.nervenets.web.hibernate.domain.user.User;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Joe on 2018/2/5.
 */
public class UserDetailHandler extends Handler<TokenParams> {
    @Override
    public String handle(TokenParams params, HttpServletRequest request) throws Exception {
        User user = userDao.findById(params.getUserId(), User.class);
        if (null == user) return errorMessage(MessageCode.code_401);
        long surplus = userDao.getSurplus(params.getUserId(), WealthType.money);
        long score = userDao.getSurplus(params.getUserId(), WealthType.score);
        long reward = userDao.getSurplus(params.getUserId(), WealthType.reward);

        UserModel userModel = new UserModel(user, 1, globalVariablesService.getGlobalRule().getDefaultAvatar());
        userModel.setSurplus(surplus);
        userModel.setScore(score);
        userModel.setReward(reward);

        return successMessage(userModel);
    }

    @Override
    public String result() throws Exception {
        return successMessage("用户信息");
    }
}
